package com.github.mzule.fantasyslide.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.github.mzule.fantasyslide.util.UserInfoUtil;

import java.util.Properties;


/**
 * Created by zhou on 2019-01-23.
 */

public class MenuActivity extends BaseActivity {
    private Button m_oLoginBtn = null;
    private Button m_oExitBtn = null;
    private EditText m_oNAME = null;
    private EditText m_oTYPE = null;
    private EditText m_oIP = null;
    private EditText m_oPort = null;
    private EditText m_oUser = null;
    private EditText m_oPsd = null;
    private EditText m_oNUM = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {

        setContentView(R.layout.menu);
        super.onCreate(savedInstanceState);
        if (!initeActivity())
        {
            this.finish();
            return;
        }
        Bundle bundle = getIntent().getExtras();
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("提示信息")
                .setPositiveButton("确定", null);
      if(bundle!=null) {
          if (bundle.getInt("code") == -1) {
              builder.setMessage("设备信息有误！").show();
          }
          if (bundle.getInt("code") == -2) {
              builder.setMessage("摄像头访问地址有误！").show();
          }
      }
        Properties properties = UserInfoUtil.getProObject(MenuActivity.this);
if(properties!=null) {
    m_oNAME.setText(properties.getProperty("name"));
    m_oTYPE.setText(properties.getProperty("type"));
    m_oIP.setText(properties.getProperty("ip"));
    m_oPort.setText(properties.getProperty("port"));
    m_oUser.setText(properties.getProperty("username"));
    m_oPsd.setText(properties.getProperty("password"));
    m_oNUM.setText(properties.getProperty("num"));
}else{
    m_oTYPE.setText("IP/Domain");
    m_oNUM.setText("1");
    }
    }
    private boolean initeActivity()
    {
        findViews();
        setListeners();
        return true;
    }
    private void findViews()
    {
        m_oLoginBtn = (Button) findViewById(R.id.btn_Login);
        m_oExitBtn = (Button) findViewById(R.id.btn_Exit);
        m_oIP = (EditText) findViewById(R.id.EDT_IP);
        m_oPort = (EditText) findViewById(R.id.EDT_Port);
        m_oUser = (EditText) findViewById(R.id.EDT_User);
        m_oPsd = (EditText) findViewById(R.id.EDT_Psd);
        m_oNAME =(EditText) findViewById(R.id.EDT_NAME);
        m_oTYPE = (EditText) findViewById(R.id.EDT_TYPE);
        m_oNUM = (EditText) findViewById(R.id.EDT_NUM);

    }

    private void setListeners()
    {
        m_oLoginBtn.setOnClickListener(Login_Listener);
        m_oExitBtn.setOnClickListener(Exit_Listener);
    }
    private Button.OnClickListener Login_Listener = new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            Intent intent = new Intent(MenuActivity.this, ViewActivity.class);
//            Intent intent=new Intent();
            String name = ((EditText)findViewById(R.id.EDT_NAME)).getText().toString();
            String type = ((EditText)findViewById(R.id.EDT_TYPE)).getText().toString();
            String num = ((EditText)findViewById(R.id.EDT_NUM)).getText().toString();
            String ip = ((EditText)findViewById(R.id.EDT_IP)).getText().toString();
            String port = ((EditText)findViewById(R.id.EDT_Port)).getText().toString();
            String user = ((EditText)findViewById(R.id.EDT_User)).getText().toString();
            String password = ((EditText)findViewById(R.id.EDT_Psd)).getText().toString();
            Bundle bundle = new Bundle();
            bundle.putString("name", name);
            bundle.putString("type", type);
            bundle.putString("num", num);
            bundle.putString("ip", ip);
            bundle.putString("port", port);
            bundle.putString("user", user);
            bundle.putString("password", password);
            UserInfoUtil.saveProUserInfo(MenuActivity.this,bundle);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();

        }
    };

    private Button.OnClickListener Exit_Listener = new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            MenuActivity.this.exitDialog();    //调用退出的方法
        }
    };

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if(keyCode == KeyEvent.KEYCODE_BACK){ //如果按了返回键

    MenuActivity.this.exitDialog();//调用退出的方法

 }

return false;

 }

    private void exitDialog(){ //退出程序的方法

Dialog dialog = new AlertDialog.Builder(MenuActivity.this)

 .setTitle("程序退出？")// 创建标题

 .setMessage("您确定要退出吗？") //表示对话框的内容


 .setPositiveButton("确定", new DialogInterface.OnClickListener() {

 public void onClick(DialogInterface dialog, int which) {

     MenuActivity.this.finish(); //操作结束

}

}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

 }

}).create(); //创建对话框

dialog.show();//显示对话框

 }

}
